package cn.gok.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class MyThread extends Thread{
    @Override
    public void run() {
        while (true){
            System.out.println("方法一创建的线程对象，执行run方法");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class A implements Runnable{

    @Override
    public void run() {
        System.out.println("方法二创建的线程对象，执行run方法");
    }
}
class B implements Callable<Integer>{
    private Integer i = 0;
    @Override
    public Integer call() throws Exception {
        i++;
        Thread.sleep(5000);
        return i;
    }
}
public class 创建线程 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyThread t1 = new MyThread();
        //t1.start();
        Runnable a = new A();
        //传入Thread构造，用thread对象启动线程
        //new Thread(a).start();
        Callable<Integer> b = new B();
        FutureTask<Integer> task = new FutureTask<>(b);
        new Thread(task).start();
        System.out.println("task线程执行完毕，结果:"+task.get());
    }
}
